package yunwo.cn.yzstation.presenter;

import android.content.Context;
import android.content.SharedPreferences;

import yunwo.cn.yzstation.api.ImainView;
import yunwo.cn.yzstation.api.ImainModel;
import yunwo.cn.yzstation.base.BasePresenter;
import yunwo.cn.yzstation.bean.UserInfoBean;
import yunwo.cn.yzstation.model.MainActivityModel;
import yunwo.cn.yzstation.utils.StringUtil;
import yunwo.cn.yzstation.view.activity.MainActivity;

/**
 * Created by lao on 2017/9/7.
 */

public class MainActivityPersenter extends BasePresenter<ImainView> implements ImainModel {
    private final SharedPreferences mInformation;
    private final SharedPreferences.Editor mInformationedit;
    MainActivity mainActivity;
    ImainView imainView;
    private final MainActivityModel mMainActivityModel;

    public MainActivityPersenter(MainActivity mainActivity, ImainView imainView) {
        attachView(imainView);
        this.mainActivity = mainActivity;
        this.imainView = imainView;
        mMainActivityModel = new MainActivityModel(mainActivity,this);

        mInformation = mainActivity.getSharedPreferences("information", Context.MODE_PRIVATE);
        mInformationedit = mInformation.edit();
    }

    public void setPersonIcon() {

        String path = "";
        if (!StringUtil.isEmpty(mInformation.getString("LocalPic", ""))) {
            path = mInformation.getString("LocalPic", "");
        } else if (!StringUtil.isEmpty(mInformation.getString("NetPic", ""))) {
            path = mInformation.getString("NetPic", "");
        }
        if (!StringUtil.isEmpty(path)) {
            mPresenterView.showIconImage(path);
        }
    }

    public void getUserInfo() {

        mMainActivityModel.getUserInfo();

    }



    public void getDictionariesData() {

        mMainActivityModel.getDictionariesData();
    }

    @Override
    public void requestUserInfoSuccess(UserInfoBean.DataBean dataBean) {
        mInformationedit.putString("username", dataBean.getNickname());
        mInformationedit.putString("mobile", dataBean.getMobile());
        mInformationedit.putString("NetPic", dataBean.getPhoto());
        mInformationedit.putString("organName",dataBean.getOrganName());
        mInformationedit.putString("organId",dataBean.getOrganId());
        mInformationedit.putString("OrganType", dataBean.getOrganType());
        String photo = dataBean.getPhoto();
        mPresenterView.showIconImage(photo);
        mInformationedit.commit();
        imainView.requestUserInfoSuccess();
    }
}
